package com.elitel.base.entity.sqlparse;

import com.elitel.common.utils.StringUtils;
import lombok.Data;
import org.apache.ibatis.type.ArrayTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

/**
 * SQL输入参数类
 * 该类主要用于封装传递给操作的参数，有助于更好地管理和理解操作中的参数，增强代码的可读性和可维护性。
 * @author xqh
 */
@Data
public class InputParam {
    /**
     * 参数名称
     */
    private String name;
    /**
     * 参数类型
     */
    private ParamType type;
    /**
     * 参数来源
     */
    private String source;
    /**
     * 是否必须
     */
    private boolean required;
    /**
     * jdbc类型
     */
    private String jdbcType;
    /**
     * java类型
     */
    private String javaType;

    // 常量定义
    private static final String JDBC_TYPE_ARRAY = "ARRAY";

    public InputParam(String name, ParamType type, String source, boolean required) {
        this.name = name;
        this.type = type;
        this.source = source;
        this.required = required;
    }

    public InputParam(String name, ParamType type, String source, boolean required, String jdbcType) {
        this.name = name;
        this.type = type;
        this.source = source;
        this.required = required;
        this.jdbcType = jdbcType;
        if (StringUtils.isNotEmpty(jdbcType)) {
            TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
            //页面输入的jdbc类型必须是JDBC类型 要求全大写
                TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(JdbcType.valueOf(jdbcType));
            Class<?> clazz = null;
            try {
                if (typeHandler instanceof ArrayTypeHandler) {
                    this.javaType = "List";
                } else {
                    String typeClassName = typeHandler.toString();
                    typeClassName = typeClassName.replace("class ", "").trim();
                    clazz = Class.forName(typeClassName);
                    //目前沒有解析#{}中的JavaType;
                    this.javaType = clazz.getSimpleName();
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }else {
            this.javaType = "String";
        }
    }

    public InputParam(String name, ParamType type, String source, boolean required, String jdbcType, String javaType) {
        this.name = name;
        this.type = type;
        this.source = source;
        this.required = required;
        this.jdbcType = jdbcType;

        //设置foreach内部标签中参数的javaType
        // 根据 jdbcType 设置 javaType
        if (JDBC_TYPE_ARRAY.equals(jdbcType)) {
            if ("String".equals(javaType)) {
                this.javaType = "String[]";
            } else if ("Integer".equals(javaType)) {
                this.javaType = "Number[]";
            } else if ("BigDecimal".equals(javaType)) {
                this.javaType = "Number[]";
            }else if ("Long".equals(javaType)) {
                this.javaType = "Number[]";
            } else {
                // 处理其他情况
                this.javaType = "String[]";
            }
        } else {
            this.javaType = javaType;
        }


    }
}